package com.moon.common;

import android.app.Application;
import android.content.Context;

import com.google.android.apps.analytics.GoogleAnalyticsTracker;

/**
 * google 分析会话管理工具
 * 
 * @author paul
 * @date 2014年5月2日
 */
public class ManagedGoogleTracker {


    protected static final String         UA_ACCOUNT    = "UA-45900170-1";

    protected static ManagedGoogleTracker INSTANCE;
    protected static Object               lock          = new Object();

    protected int                         activityCount = 0;
    protected Integer                     dispatchIntervalSecs;
    protected Context                     context;

    /**
     * NOTE: you should use your Application context, not your Activity context,
     * in order to avoid memory leaks.
     */
    protected ManagedGoogleTracker(Application context) {
        this.context = context;
    }

    /**
     * NOTE: you should use your Application context, not your Activity context,
     * in order to avoid memory leaks.
     */
    protected ManagedGoogleTracker(int dispatchIntervalSecs, Application context) {
        this.dispatchIntervalSecs = dispatchIntervalSecs;
        this.context = context;
    }

    /**
     * This should be called once in onCreate() for each of your activities that
     * use GoogleAnalytics. These methods are not synchronized and don't
     * generally need to be, so if you want to do anything unusual you should
     * synchronize them yourself.
     */
    public void increaseActivityCount() {
        if (activityCount == 0)
            if (dispatchIntervalSecs == null) {
                GoogleAnalyticsTracker.getInstance().startNewSession(UA_ACCOUNT, context);
            } else {
                GoogleAnalyticsTracker.getInstance().startNewSession(UA_ACCOUNT,
                        dispatchIntervalSecs, context);
            }
        activityCount++;
    }

    /**
     * This should be called once in onDestrkg() for each of your activities
     * that use GoogleAnalytics. These methods are not synchronized and don't
     * generally need to be, so if you want to do anything unusual you should
     * synchronize them yourself.
     */
    public void decreaseActivityCount() {
        activityCount = Math.max(activityCount - 1, 0);
        // 提交一次
        dispatch();

        if (activityCount == 0) {
            GoogleAnalyticsTracker.getInstance().stopSession();
        }
    }

    public void setCustomVar(int index, String name, String value, int scope) {
        GoogleAnalyticsTracker.getInstance().setCustomVar(index, name, value, scope);
    }

    public void trackEvent(String category, String action, String label, int value) {
        GoogleAnalyticsTracker.getInstance().trackEvent(category, action, label, value);
    }

    public void trackPageView(String pagePath) {
        GoogleAnalyticsTracker.getInstance().trackPageView(pagePath);
    }

    public void dispatch() {
        GoogleAnalyticsTracker.getInstance().dispatch();
    }

    /**
     * Get or create an instance of GoogleAnalyticsSessionManager
     */
    public static ManagedGoogleTracker getInstance(Application context) {
        if (INSTANCE == null) {
            synchronized (lock) {
                if (INSTANCE == null) {
                    INSTANCE = new ManagedGoogleTracker(context);
                }
            }
        }
        return INSTANCE;
    }

    public static ManagedGoogleTracker getInstanceAndIncreaseCount(Application context) {
        ManagedGoogleTracker instance = getInstance(context);
        instance.increaseActivityCount();
        return instance;
    }

}
